<?php

namespace app\logic\single\pay;

use Alipay\EasySDK\Kernel\Factory;
use Alipay\EasySDK\Kernel\Config;

class AliPaySingle
{
    private static $inc;

    private function __clone(){

    }
    private function __construct()
    {
        Factory::setOptions($this->getOptions());
    }

    public static function getInstance(): AliPaySingle
    {
        if(is_null(self::$inc)){
            self::$inc = new self();
        }
        return self::$inc;
    }
    function pay($order=[],$terminal='h5',$type='music',$title='购买音乐',$quit_url='',$return_url=''){
        $notify="https://api.jcdjpool.com/api/notify/ali/{$type}_notify";
        try {
            if($terminal == 'h5'){
                $result = Factory::payment()
                    ->wap()
                    ->asyncNotify($notify)
                    ->pay(
                        $title,
                        $order['order_no'],
                        $order['pay_price'],
                        $quit_url,
                        $return_url,
                    );
            }else{
                $result = Factory::payment()
                    ->page()
                    ->asyncNotify($notify)
                    ->pay(
                        $title,
                        $order['order_no'],
                        $order['pay_price'],
                        $return_url,
                    );
            }
            //3. 处理响应或异常
            return ['code'=>1,'body'=>base64_encode($result->body),'msg'=>'success'];
        } catch (Exception $e) {
            return ['code'=>-1,'msg'=>"调用失败，". $e->getMessage(). PHP_EOL];
        }
    }

    function verifyNotify($param): bool
    {
        return Factory::payment()->common()->verifyNotify($param);
    }
    private function getOptions(): Config
    {
        $options = new Config();
        $options->protocol = 'https';
        $options->gatewayHost = 'openapi.alipay.com';
        $options->signType = 'RSA2';

        $options->appId = '2021004144659105';

        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
        $options->merchantPrivateKey = 'MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCcCeGHWK3IPEtUZ6y2gT6C5cL9PlZ3gMgaWDb2L6XcedHTjcWDwHIMzPqxRs2NcFyhowTOLRgyV3XbSQvmzPRpWQ4wZxNvYNPZLd4uApRuQ3M+xUhBYPrA6UbgI5KrPPDZV6vIIS9MwedGEqPIMDnwjy4QbvneXVMcAMaFWgFYxLZDQWEicFfzKkJIMYsmzRBheEqsveJ61jeSZlK77U3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AkblwvWtYSeeimLeKxQV5oThVUyTKygRHwiB7bApOx7jmOsMsMZ38sEgMOvwihja0uhWxQaaS5UBmT4CWKIbiM/uJVBn67gQ6k+/MbASDeeojUMCgYBsN2DinEJXCL2qQP7NILkE+RZFfrzdqibU1WjmUTs5m/DDhN98xYiGNB80DP15eLsRdnaxe7FgO6BXbWZAKc/0vGnoXQk2hAYM8QqQ6lWwMNMMtbmZUo5uhFoFaK7mtiJdlmgM+1HWHSYxUe758uxfDEEntF79m3ByGwD5GqybLg==';

        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
        $options->alipayPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAn2sHXFqr1ut3AOPk4K4VMxr6TG0A+WcmlGAyh2+kbrOTJ6IqGLXeJbZBE1AySsx7wFc70NGpr6K+5pc8e8nJlPVDBkk/O71rMZP8pn56yCimFVhUWdVwngwVh3QvxWSZqt89/bwVYkcnAoBgpsnzBN/8lemMV6hzBOaAin/kk11wT0diQd9ivis1weFWbmVuY4lYohbaELbCD4or16t3pEo83bND+wx4RFD604aiwZ9v8C0ggNyh+2UMasvaD7IRnGRo2J8jxdkkPgl+7fPkTaju2yRCinU492UE7cE5dfLHd9xAaqCbyh7AFpF+T0FkxxvF3c+Er9awrqmBAD+u1wIDAQAB';
        //可设置异步通知接收服务地址（可选）
        //如果需要使用文件上传接口，请不要设置该参数
        $options->notifyUrl = '';

        //可设置AES密钥，调用AES加解密相关接口时需要（可选）
        $options->encryptKey = 'dZWmUeo5foofPpKAbE7a7A==';
        return $options;
    }
}